/*
 * Copyright 2004-2008, Thorbjrn Lindeijer <thorbjorn@lindeijer.nl>
 * Copyright 2004-2008, Adam Turk <aturk@biggeruniverse.com>
 *
 * This file is part of libtiled-java.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package tiled.core;

import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;

/**
 * An object occupying an {@link ObjectGroup}.
 */
public class MapObject implements Cloneable {
	private Properties	properties	= new Properties();
	private ObjectGroup	objectGroup;
	private Rectangle	bounds		= new Rectangle();
	private String		name		= "Object";
	private String		type		= "";
	private String		imageSource	= "";
	private Image		image;
	private Image		scaledImage;

	public MapObject(int x, int y, int width, int height) {
		this.bounds = new Rectangle(x, y, width, height);
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		MapObject clone = (MapObject) super.clone();
		clone.bounds = new Rectangle(this.bounds);
		clone.properties = (Properties) this.properties.clone();
		return clone;
	}

	/**
	 * @return the object group this object is part of
	 */
	public ObjectGroup getObjectGroup() {
		return this.objectGroup;
	}

	/**
	 * Sets the object group this object is part of. Should only be called by the object group.
	 * 
	 * @param objectGroup
	 *            the object group this object is part of
	 */
	public void setObjectGroup(ObjectGroup objectGroup) {
		this.objectGroup = objectGroup;
	}

	public Rectangle getBounds() {
		return this.bounds;
	}

	public void setBounds(Rectangle bounds) {
		this.bounds = bounds;
	}

	public String getImageSource() {
		return this.imageSource;
	}

	public void setImageSource(String source) {
		if (this.imageSource.equals(source)) {
			return;
		}

		this.imageSource = source;

		// Attempt to read the image
		if (this.imageSource.length() > 0) {
			try {
				this.image = ImageIO.read(new File(this.imageSource));
			} catch (IOException e) {
				this.image = null;
			}
		} else {
			this.image = null;
		}

		this.scaledImage = null;
	}

	/**
	 * Returns the image to be used when drawing this object. This image is scaled to the size of the object.
	 * 
	 * @param zoom
	 *            the requested zoom level of the image
	 * @return the image to be used when drawing this object
	 */
	public Image getImage(double zoom) {
		if (this.image == null) {
			return null;
		}

		final int zoomedWidth = (int) (this.getWidth() * zoom);
		final int zoomedHeight = (int) (this.getHeight() * zoom);

		if ((this.scaledImage == null) || (this.scaledImage.getWidth(null) != zoomedWidth) || (this.scaledImage.getHeight(null) != zoomedHeight)) {
			this.scaledImage = this.image.getScaledInstance(zoomedWidth, zoomedHeight, Image.SCALE_SMOOTH);
		}

		return this.scaledImage;
	}

	public int getX() {
		return this.bounds.x;
	}

	public void setX(int x) {
		this.bounds.x = x;
	}

	public int getY() {
		return this.bounds.y;
	}

	public void setY(int y) {
		this.bounds.y = y;
	}

	public void translate(int dx, int dy) {
		this.bounds.translate(dx, dy);
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getWidth() {
		return this.bounds.width;
	}

	public void setWidth(int width) {
		this.bounds.width = width;
	}

	public void setHeight(int height) {
		this.bounds.height = height;
	}

	public int getHeight() {
		return this.bounds.height;
	}

	public Properties getProperties() {
		return this.properties;
	}

	public void setProperties(Properties p) {
		this.properties = p;
	}

	@Override
	public String toString() {
		return this.type + " (" + this.getX() + "," + this.getY() + ")";
	}
}
